import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import statsmodels.api as sm

# 设置中文字体的显示
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
data2 = pd.read_csv('data2.csv', index_col="Date")
data2.index = [dt.datetime.strptime(x, "%Y-%m-%d") for x in data2.index]

# print(data2.head())
# print(data2.iloc[0])
# quit()
# 证券价格归一化
# (data2 / data2.iloc[0] * 100).plot(figsize=(10, 6))
# plt.xlabel("股价")
# plt.legend(loc="upper left")
# plt.show()
# 计算证券对数收益率，下面这个pct_change函数是计算收益率的
log_returns = np.log(data2.pct_change() + 1)

# print(log_returns.head())
# 绘制收益直方图
# log_returns.hist(bins=50, figsize=(10, 6), layout=(2, 3))
# plt.show()
fig, axes = plt.subplots(3, 2, figsize=(10, 12))
for i in range(0, 3):
    for j in range(0, 2):
        # 绘制样本分位图
        sm.qqplot(log_returns.iloc[:, 2 * i + j].dropna(),
                  line='s',
                  ax=axes[i, j])
        axes[i, j].grid(True)
        axes[i, j].set_title(log_returns.columns[2 * i + j])
        axes[i, j].set_xlabel('理论分位数')
        axes[i, j].set_ylabel('样本分位数')
plt.subplots_adjust(wspace=0.3, hspace=0.4)
plt.show()
